/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "workerprocess.h"
#include "freetrackdialog.h"
#include "freetrackwidget.h"
#include "ftpushbutton.h"
#include "fticonlabel.h"

bool worker_already_open = false;
WorkerProcess::WorkerProcess(int npb) :
    QObject(0)
{
    please_terminate = still_running = error = false;
    n_progress_bars = npb;
}

bool WorkerProcess::run(const QString &title)
{
    if ( worker_already_open )
        return false;

    worker_already_open = true;

    // Build dialog box
    FreeTrackWidget* widget = new FreeTrackWidget();
    FreeTrackDialog* dialog = new FreeTrackDialog( widget );

    // Build widget layout
    QVBoxLayout* layout = new QVBoxLayout(widget);

    // Set title
    FTLabel* wait_title = new FTLabel(title);
    layout->addWidget( wait_title );
    layout->addStretch();

    // Set all progress bars...
    for (int p = 0; p < n_progress_bars; p++ )
    {
        QProgressBar* wait_progress = new QProgressBar();
        wait_progress->setMinimum(0);
        wait_progress->setMaximum(0);
        wait_progress->setValue(0);
        wait_progress->hide();

        FTLabel* wait_progress_label = new FTLabel();
        wait_progress_label->hide();


        layout->addWidget( wait_progress );
        layout->addWidget( wait_progress_label );

        wait_progresses.append( wait_progress );
        wait_progress_labels.append( wait_progress_label );
    }

    // Set the wait icon...
    QHBoxLayout* hor = new QHBoxLayout();
    FTIconLabel* wait_icon = new FTIconLabel( QPixmap( ":/icons/wait.png") );
    hor->addStretch();
    hor->addWidget( wait_icon );
    hor->addStretch();
    layout->addStretch();
    layout->addLayout( hor );

    // Abort button
    layout->addStretch();
    FTPushButton* wait_cancel = new FTPushButton( tr("Abort...") );
    layout->addWidget( wait_cancel );

    // Signal worker when the user select "reject"
    connect( wait_cancel, SIGNAL(clicked()), this, SLOT(pleaseTerminate()));

    // Let execute the queue of events and show!
    qApp->processEvents( QEventLoop::AllEvents );
    dialog->showFullScreen();
    qApp->processEvents( QEventLoop::AllEvents );

    bool run_status_ok = false;

    please_terminate = error = false;
    still_running = true;
    run_status_ok  = do_stuff();
    still_running = false;
    if ( !run_status_ok )
        QMessageBox::critical(qApp->activeWindow(), tr("An error has occurred!"), error_string );

    dialog->hide();
    qApp->processEvents( QEventLoop::AllEvents );
    worker_already_open = false;
    delete dialog;
    return run_status_ok;
}

bool WorkerProcess::do_stuff()
{
    return true;
}

bool WorkerProcess::isTerminated()
{
    return still_running;
}

void WorkerProcess::pleaseTerminate()
{
    please_terminate = true;
}

QString WorkerProcess::getErrorString()
{
    return error_string;
}

void WorkerProcess::setErrorString(const QString& se )
{
    error_string = se;
    error = true;
}

void WorkerProcess::sendUpdate(const QString &update, int who, int min, int max, int curr)
{
    if ( (who < 0) || (who > n_progress_bars ) )
        return;

    QProgressBar* wait_progress = wait_progresses.at(who);
    FTLabel* wait_progress_label = wait_progress_labels.at(who);

    wait_progress->show();
    wait_progress_label->show();

    wait_progress->setMinimum(min);
    wait_progress->setMaximum(max);
    wait_progress->setValue(curr);
    if ( update != "" )
        wait_progress_label->setText( update );

    qApp->processEvents( QEventLoop::AllEvents );
}
